package com.smartstate.EnvironmentMonitor.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.smartstate.EnvironmentMonitor.dto.PageDTO;
import com.smartstate.EnvironmentMonitor.entity.Systemmonitor;
import com.smartstate.EnvironmentMonitor.mapper.SystemmonitorMapper;
import com.smartstate.EnvironmentMonitor.service.SystemmonitorService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class SystemmonitorServiceImpl extends ServiceImpl<SystemmonitorMapper, Systemmonitor> implements SystemmonitorService {


    @Override
    public Page<Systemmonitor> queryAllPageByDTO(PageDTO pageDTO) {
        return null;
    }

    @Resource
    private SystemmonitorMapper systemmonitorMapper;

    @Resource
    private RedisTemplate redisTemplate;

    /**
     * 读取设备ip和状态
     * @return
     */
    public List<Systemmonitor> getDeviceStatus() {
        Map<String, Map> mapDeviceIds = systemmonitorMapper.getDeviceIds();
        List<Systemmonitor> systemmonitorList = mapDeviceIds.keySet().stream().map((key) -> {
            Systemmonitor systemmonitor = new Systemmonitor();
            String ip = (String) redisTemplate.opsForHash().get(key, "ip");
            String type = (String) redisTemplate.opsForHash().get(key, "type");
            if ("0".equals(type)){
                type = "离线";
                ip = "";
            }
            Map map = (Map) mapDeviceIds.get(key);
            String way = String.valueOf(map.get("way")).toUpperCase();
            String littleParticle = (String) redisTemplate.opsForHash().get(key, "littleParticle");
            String dewPoint = (String) redisTemplate.opsForHash().get(key, "dewPoint");
            String humidity = (String) redisTemplate.opsForHash().get(key, "humidity");
            String staticPressure = (String) redisTemplate.opsForHash().get(key, "staticPressure");
            String longParticle = (String) redisTemplate.opsForHash().get(key, "longParticle");
            String temperature = (String) redisTemplate.opsForHash().get(key, "temperature");
            //-10086.11代表读不到数据
            if ("-10086.11".equals(longParticle) || "-10086.11".equals(littleParticle)) {
                systemmonitor.setParticleType("异常");
            } else if (longParticle != null){
                systemmonitor.setParticleType("正常");
            }
            if ("-10086.11".equals(dewPoint)) {
                systemmonitor.setDewPointType("异常");
            } else if (dewPoint != null){
                systemmonitor.setDewPointType("正常");
            }
            if ("-10086.11".equals(staticPressure)) {
                systemmonitor.setPressureType("异常");
            } else if (staticPressure != null){
                systemmonitor.setPressureType("正常");
            }
            if ("-10086.11".equals(humidity) || "-10086.11".equals(temperature)) {
                systemmonitor.setTemperatureAndHumidityType("异常");
            } else if (humidity != null){
                systemmonitor.setTemperatureAndHumidityType("正常");
            }
            systemmonitor.setWay(way);
            systemmonitor.setIp(ip);
            systemmonitor.setDeviceId(key);
            systemmonitor.setType(type);
            return systemmonitor;
        }).collect(Collectors.toList());
        return systemmonitorList;
    }
}
